/**
 * @param {Array} list
 * @param {string} showName 下拉需要显示的name
 * @returns {string} code 下拉需要传给接口的code
 */
export function filterData(list, showName, code) {
  for (let i = 0; i < list.length; i++) {
    list[i]['label'] = list[i][showName]
    list[i]['value'] = list[i][code]
  }
  return list
}

export function deepClone(source) {
  const targetObj = source.constructor === Array ? [] : {} // 判断复制的目标是数组还是对象
  for (let keys in source) { // 遍历目标
    if (source[keys] && typeof source[keys] === 'object') { // 如果值是对象或者数组，就递归一下
      targetObj[keys] = deepClone(source[keys])
    } else { // 如果不是，就直接赋值
      targetObj[keys] = source[keys]
    }
  }
  return targetObj
}
export const debounce = (func, wait = 500) => {
  let timeout
  return function() {
    const context = this
    const args = [...arguments]
    if (timeout) clearTimeout(timeout)
    timeout = setTimeout(() => {
      func.apply(context, args)
    }, wait)
  }
}
